<?
class Sou
{
	var $obj;
	var $ele;
	var $ct;
	var $display;
	//Constructor: It accepts the controller name as parameter.
	//the Controller should initialize this object and will be subsequently used by actions under it 
	public function __construct($ct="default")
	{
		$this->ct=$ct;
	}
	
	
	//assign an object to template
	public function assign($name,$obj)
	{
			$this->ele[$name]=$obj;
	}
	
	//render the page
	public function display($file)
	{
		if($file==null) return;
		
		
		//lets read the super template only if it exists and is not empty
		$filename = "tl/layout/".$this->ct.".php";
		if(filesize($filename)>0)
		{
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			fclose($handle);
		}
		
		//lets read the controller parent template
		$filename = "tl/".$this->ct."/".$file.".php";
		if(!file_exists($filename))
		{
			die("template {$file} does not exist");
		}
		if(filesize($filename)>0)
		{
			$handle = fopen($filename, "r");
			$contents .= fread($handle, filesize($filename));
			fclose($handle);
		}
		if(is_array($this->ele))
		{
			foreach($this->ele as $k=>$v)
			{
				$$k=$v;
			}
		}
		if(!empty($contents))
		{
			$contents="?>".$contents."<?";
		}
		$this->display=$contents;
		//render the super template		
		include_once "tl/layout/super.php";

			
	}
	//function content
	//the display class renders the data, we need to have some function to just return the html rather than rendering it
	function get_display()
	{
		if($file==null) return;
		
		
		//lets read the super template only if it exists and is not empty
		$filename = "tl/layout/".$this->ct.".php";
		if(filesize($filename)>0)
		{
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			fclose($handle);
		}
		
		//lets read the controller parent template
		$filename = "tl/".$this->ct."/".$file.".php";
		if(!file_exists($filename))
		{
			die("template {$file} does not exist");
		}
		if(filesize($filename)>0)
		{
			$handle = fopen($filename, "r");
			$contents .= fread($handle, filesize($filename));
			fclose($handle);
		}
		foreach($this->ele as $k=>$v)
		{
			$$k=$v;
		}
		if(!empty($contents))
		{
			$contents="?>".$contents."<?";
		}
		$this->display=$contents;		
		return $this->display;
	}
}
?>